<?php

namespace App\Http\Controllers;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Model\Message;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Config;

class MessageController extends Controller
{

    public function __construct()
    {

    }

    public function index(Request $request)
    {
        $email = $request->input('email');
        $name = $request->input('name');
        $subject = $request->input('subject');
        $message = $request->input('message');


        $validator = Validator::make($request->all(), [
                'email'   => 'required|email',
                'name'    => 'required',
            ],[
                'email.required'=>'请填写邮箱',
                'email.email'=>'请填写正确邮箱',
                'name.required'=>'请填写姓名',
            ]
        );

        if($validator->fails()){
            $msg = $validator->errors()->first();
            return response()->json(['status'=>0,'errMsg'=>$msg]);
        }
//        $model = new Message();
//        $model->email = $email;
//        $model->name = $name;
//        $model->subject = $subject;
//        $model->message = $message;
//        $model->save();

        //发送邮件
        $data = array(
            'Email'=>$email,
            'Name'=>$name,
            'Subject'=>$subject,
            'Message'=>$message,
        );

        Mail::send('emails.new', $data, function ($message) {
            $message->to(Config::get('mail.username'), '我')->subject('您有一个新消息');
        });
        return response()->json(['status'=>1,'errMsg'=>'']);
    }

}
